package com.monitor.app_monitor.service.quartz.task;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.monitor.app_monitor.entity.CellectInfo;
import com.monitor.app_monitor.entity.FileinfoData;
import com.monitor.app_monitor.entity.RealTimeAlarmDo;
import com.monitor.app_monitor.entity.TaskLogAnnotation;
import com.monitor.app_monitor.entity.mapper.RealTimeAlarmClickhouseMapper;
import com.monitor.app_monitor.entity.mapper.RealTimeAlarmMapper;
import com.monitor.app_monitor.service.quartz.utils.ExecuteScript;
import com.monitor.app_monitor.service.quartz.utils.FileinfoScriptExecResult;
import com.monitor.app_monitor.service.quartz.utils.logUtils;
import io.micrometer.core.instrument.ImmutableTag;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @ClassName CellectDataCheck.java
 * @Author ning
 * @Description TODO
 * @CreateTime 2021年12月16日 10:18:00
 */

@DisallowConcurrentExecution //作业不并发
@Component
@Slf4j
public class AlarmNbMysql implements Job {

    @Resource
    @Qualifier("realTimeAlarmMap")
    private Map<String, AtomicInteger> realTimeAlarmMap;

    @Autowired
    RealTimeAlarmMapper realTimeAlarmMapper;


    @Resource
    @Qualifier("realTimeAlarmClickMap")
    private Map<String, AtomicInteger> realTimeAlarmClickMap;

    @Autowired
    MeterRegistry registry;

    @Autowired
    RealTimeAlarmClickhouseMapper realTimeAlarmClickhouseMapper;

    @TaskLogAnnotation
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        /**
         * 监控告警mysql和clickhosue数据库的数据量
         */


        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        String nowData = sdf.format(date);

        List<RealTimeAlarmDo> realTimeAlarms = realTimeAlarmMapper.findAll(nowData);

        List<RealTimeAlarmDo> realTimeAlarmClickhouses = realTimeAlarmClickhouseMapper.findAllClick(nowData);

        realTimeAlarms.forEach(realTimeAlarm -> {
            int resCode = 0;
            resCode = realTimeAlarm.getEmsNb();

            if (realTimeAlarmMap.keySet().contains(realTimeAlarm.getEmsName()+realTimeAlarm.getEmsIp()+nowData)) {
                //存在
                realTimeAlarmMap.get(realTimeAlarm.getEmsName()+realTimeAlarm.getEmsIp()+nowData).set(resCode);
            } else {
                //不存在
                List<Tag> tags = new ArrayList<>();
                tags.add(new ImmutableTag("emsName",realTimeAlarm.getEmsName()));
                tags.add(new ImmutableTag("emsIp",realTimeAlarm.getEmsIp()));
                tags.add(new ImmutableTag("Data",nowData));
                AtomicInteger list = registry.gauge("clarm_mysql", tags, new AtomicInteger(realTimeAlarm.getEmsNb()));
                realTimeAlarmMap.put(realTimeAlarm.getEmsName()+realTimeAlarm.getEmsIp()+nowData,list);
            }
        });

        realTimeAlarmClickhouses.forEach(realTimeAlarmClickhouse -> {
            int resCode = 0;
            resCode = realTimeAlarmClickhouse.getEmsNb();

            if (realTimeAlarmClickMap.containsKey(realTimeAlarmClickhouse.getEmsName()+realTimeAlarmClickhouse.getEmsIp()+nowData)) {
                //存在
                realTimeAlarmClickMap.get(realTimeAlarmClickhouse.getEmsName()+realTimeAlarmClickhouse.getEmsIp()+nowData).set(resCode);
            } else {
                //不存在
                List<Tag> tags = new ArrayList<>();
                tags.add(new ImmutableTag("emsName",realTimeAlarmClickhouse.getEmsName()));
                tags.add(new ImmutableTag("emsIp",realTimeAlarmClickhouse.getEmsIp()));
                tags.add(new ImmutableTag("Data",nowData));
                AtomicInteger list = registry.gauge("clarm_clickhouse", tags, new AtomicInteger(realTimeAlarmClickhouse.getEmsNb()));
                realTimeAlarmClickMap.put(realTimeAlarmClickhouse.getEmsName()+realTimeAlarmClickhouse.getEmsIp()+nowData,list);
            }
        });
    }
}
